<?php
/**
 * BasicPHP-FF
 * 杂项功能（无需实例化，直接静态调用）
 * @author wangji <j@wj45.net>
 */
 
class Misc
{
    private function __construct() {}
    
    //生成分页Html（无需实例化，直接静态调用）
    public static function getPageHtml($currentPage, $totalCount, $perCount = 10, $url = '/') {
    	$html = array();
    	if ($totalCount > 0 && $perCount > 0) {
    		$max_page = ceil($totalCount / $perCount);
    	} else {
    		$max_page = 1;
    	}
        if (!$currentPage) {
    		$currentPage = 1;
    	}
        if($currentPage > 1) {
    		$html[] = "<a href='{$url}'>首页</a>";
    		$html[] = "<a href='{$url}".($currentPage-1)."'>上一页</a>";
    	}
    	for ($i = $currentPage - 3; $i <= $currentPage - 1; $i++) {
    		if ($i < 1) continue;
    		$html[] = "<a href='{$url}{$i}'>{$i}</a>";
    	}
    	$html[] = "<span class='current'>{$currentPage}</span>";
    	if ($currentPage < $max_page) {
    		$flag = 0;
    		for ($i = $currentPage + 1; $i <= $max_page; $i++) {
    			$html[] = "<a href=\"{$url}$i\">$i</a>";
    			$flag++;
    			if ($flag == 4) break;
    		}
    		$html[] = "<a href='{$url}".($currentPage+1)."'>下一页</a>";
    		$html[] = "<a href='{$url}{$max_page}'>末页</a>";
    	}
		$html[] = "<span>共{$max_page}页</span>";
    	return implode(' ', $html);
    }
    
    //utf-8截字（1个汉字当做2个字符）
	public static function subString($string, $length = 50) {
        $rs = '';
	    for ($i = 0; $i < $length; $i++) {
            $tmp = substr($string, 0, 1);
            if (ord($tmp) > 127) {
                $i++;
                if ($i < $length) {
                    $rs .= substr($string, 0, 3);
                    $string = substr($string, 3);
                }
            } else {
                $rs .= substr($string, 0, 1);
                $string = substr($string, 1);
            }
        }
		return $rs;
	}

	//URL跳转
	public static function redirect($url, $msg = '') {
		if ( !headers_sent() ) {
			header('Content-type: text/html; charset=utf-8');
			header("Location: $url");
		}
		$str = '<script type="text/javascript">';
		$str .= "window.location='$url';";
		$str .= '</script>';
		echo $str;
		die($msg);
	}

	//生成随机字符串（大写字母和数字，不包含0、O、1、I容易混淆的字符）
	public static function random($num = 1) {
		$array = array(2, 3, 4, 5, 6, 7, 8, 9, 
					'A', 'B', 'C', 'D', 'E', 'F', 
					'G', 'H', 'J', 'K', 'L', 'M', 
					'N', 'P', 'Q', 'R', 'S', 'T', 
					'V', 'U', 'W', 'X', 'Y', 'Z' );
		$count = count($array);
		$str = '';
		for ($i = 0; $i < $num; $i++) {
			$j = rand(0, $count - 1);
			$str .= $array[$j];
		}
		return $str;
	}
    
    //给session赋值
    public static function setSession($name, $value) {
        if ( !isset($_SESSION) ) {
			@session_start();
		}
        if ( empty($value) ) {
			unset($_SESSION[$name]);
		} else {
			$_SESSION[$name] = $value;
		}
    }
    
    //使用cURL进行HTTP请求，$post不空则执行POST请求
    public static function curlExec($url, $post = '', $timeout = 10, $cookie = NULL, $referer = NULL) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        if ( !empty($post) ) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_URL, $url);
        if ( !empty($cookie) ) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        if ( !empty($referer) ) {
            curl_setopt($curl, CURLOPT_REFERER, $referer);
        }
        $rs = curl_exec($curl);
        return $rs;
    }
    
	//返回json数组，可用于同域ajax
	public static function ajaxReturn($status, $data = '') {
		return json_encode(array('status'=>$status, 'data'=>$data));
	}
}

/* ------ EOF ------ */